package one.bugu.android.demon.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import one.bugu.android.demon.R;

/**
 * Description NoScrollViewPager 可控制滑动的Viewpager
 *
 * @author liuxin
 * @date 2018/10/26 13:43
 */
public class NoScrollViewPager extends ViewPager {

    private boolean noScroll = false;

    public NoScrollViewPager(Context context) {
        this(context, null);
    }

    public NoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

        handlerAttrs(context, attrs);
    }

    /**
     * 自定义属性
     * @param context
     * @param attrs
     */
    private void handlerAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray;
        try {
            typedArray = context.obtainStyledAttributes(attrs, R.styleable.NoScrollViewPager);
        } catch (Exception e) {
            typedArray = null;
        }
        if (typedArray != null) {
            setNoScroll(typedArray.getBoolean(R.styleable.NoScrollViewPager_canScroll, false));
        }
    }

    /**
     * 设置是否滑动
     * @param noScroll
     */
    public void setNoScroll(boolean noScroll) {
        this.noScroll = noScroll;
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (!noScroll) {
            return false;
        } else {
            return super.onTouchEvent(arg0);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (!noScroll) {
            return false;
        } else {
            return super.onInterceptTouchEvent(arg0);
        }
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }
}
